package com.example.controller;

import com.example.mapper.LoginUserMapper;
import com.example.pojo.Employe;
import com.example.pojo.Employee;
import com.example.pojo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class IndexController {

    @Autowired
    LoginUserMapper loginUserMapper;

    @RequestMapping("/")
    public String index(HttpSession session){
//        Object loginUser = session.getAttribute("loginUser");
//        if(loginUser==null){
//            return "redirect:/login";
//        }
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName(); //get logged in username
        if (name.equals("anonymousUser")){
            return "redirect:/login";
        }
        session.setAttribute("loginUser", name);
//        model.addAttribute("username", name);
        return "redirect:/index";
    }

    @RequestMapping("/index")
    public  String index(){
        return "index";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request){
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName(); //get logged in username
        if (!name.equals("anonymousUser")){
            return "redirect:/index";
        }
        return "login";
    }

    @PostMapping("/toLogin")
    public String toLogin(LoginUser loginUser, HttpSession session){
        System.out.println("LoginUser" + loginUser);
        return "index";
    }

    @RequestMapping("/toLogin")
    public String toLogin(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          Model model, HttpSession session) {
        LoginUser user = loginUserMapper.userLogin(username, password);
        System.out.println("用户名密码：" + username + password);
        if (user == null) {
            model.addAttribute("msg", "用户名或密码错误");
            return "login";
        } else {
            session.setAttribute("loginUser", username);
            return "redirect:/index";
        }
//        if (!StringUtils.isEmpty(username) && "1234".equals(password)) {
//            session.setAttribute("loginUser", username);
//            return "redirect:/main.html";
//        }
//        else{
//            model.addAttribute("msg", "用户名或密码错误");
//            return "login";
//        }
    }

    @GetMapping("/register")
    public String toRegister(){
        return "register";
    }

    @PostMapping("/register")
    public String register(@RequestParam("username") String username, @RequestParam("password") String password){
        loginUserMapper.addUser(username, password);
        return "login";
    }

    @RequestMapping("/logout")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/login";
    }
}
